// pages/community_worker/convenientdetails/convenientdetails.js
import Toast from "../../../miniprogram_npm/@vant/weapp/toast/toast";
import Dialog from "../../../miniprogram_npm/@vant/weapp/dialog/dialog";

const app = getApp()
const log = new app.appLogs()
const http = new app.appHttp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        type: '',
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        hidden: true,
        isLoading: true,
        namelist: [
            [], [], [], [], [], [], [], [], [], [], [], [], []
        ]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const id = options.id
        this.setData({
            type: id
        })
        let list = [];
        for (let i = 0; i < 26; i++) {
            list[i] = String.fromCharCode(65 + i)
        }
        this.setData({
            list: list,
            listCur: list[0]
        })
        this.getPhone();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        let that = this;
        wx.createSelectorQuery().select('.indexBar-box').boundingClientRect(function (res) {
            that.setData({
                boxTop: res.top
            })
        }).exec();
        wx.createSelectorQuery().select('.indexes').boundingClientRect(function (res) {
            that.setData({
                barTop: res.top
            })
        }).exec()
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    //获取文字信息
    getCur(e) {
        this.setData({
            hidden: false,
            listCur: this.data.list[e.target.id],
        })
    }, setCur(e) {
        this.setData({
            hidden: true,
            listCur: this.data.listCur
        })
    }, //滑动选择Item
    tMove(e) {
        let y = e.touches[0].clientY,
            offsettop = this.data.boxTop,
            that = this;
        //判断选择区域,只有在选择区才会生效
        if (y > offsettop) {
            let num = parseInt((y - offsettop) / 20);
            this.setData({
                listCur: that.data.list[num]
            })
        }
        ;
    },

    //触发全部开始选择
    tStart() {
        this.setData({
            hidden: false
        })
    },

    //触发结束选择
    tEnd() {
        this.setData({
            hidden: true,
            listCurID: this.data.listCur
        })
    },
    indexSelect(e) {
        let that = this;
        let barHeight = this.data.barHeight;
        let list = this.data.list;
        let scrollY = Math.ceil(list.length * e.detail.y / barHeight);
        for (let i = 0; i < list.length; i++) {
            if (scrollY < i + 1) {
                that.setData({
                    listCur: list[i],
                    movableY: i * 20
                })
                return false
            }
        }
    },
    /**
     * 请求便民电话详情
     */
    getPhone: function () {
        const than = this
        this.setData({
            isLoading: true
        })
        let is = true
        http.get("phone/open/list", {
            type: than.data.type,
            area_code: app.globalData.areaCode
        }).then((res) => {
            if (res.data.code == 200) {
                res.data.data.forEach((item, index) => {
                    if (item.length != 0) {
                        is = false
                    }
                })
                if (!is) {
                    than.setData({
                        namelist: res.data.data
                    })
                } else {
                    than.setData({
                        namelist: []
                    })
                }
            }
            than.setData({
                isLoading: false
            })
        })
    },
    /**
     * 电话呼叫
     */
    getPhonenum: function (event) {
        const phone = event.currentTarget.dataset.phone
        Dialog.confirm({
            title: '提示',
            message: '确认拨打' + phone + '吗？',
        }).then(() => {
                wx.makePhoneCall({
                    phoneNumber: phone
                })
            }).catch(() => {
                // on cancel
            });
    }
})